package com.focus.wecenter.web.controller;

import com.focus.wecenter.pojo.Article;
import com.focus.wecenter.pojo.ArticleComment;
import com.focus.wecenter.pojo.Topic;
import com.focus.wecenter.service.ArticleService;
import com.focus.wecenter.service.TopicService;
import com.focus.wecenter.util.FunUtil;
import com.focus.wecenter.util.Message;
import com.focus.wecenter.util.PageBean;
import com.focus.wecenter.web.controller.common.BaseController;
import com.focus.wecenter.web.vo.QuestionPublishForm;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 文章模块
 *
 * @author Lv Jing
 * @date 2018年01月08日 16:35
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private TopicService topicService;
    @Value("${article.pagesize}")
    private int pageSize;

    /**
     * 显示发布页面
     */
    @RequestMapping("/publish")
    public String showPublish() {
        return "article/publish";
    }

    @RequestMapping(value = "/ajax/publish", method = RequestMethod.POST)
    @ResponseBody
    public Message publish(QuestionPublishForm questionPublishForm, HttpSession session) {
        Message message = new Message();
        // 标题检查
        if (FunUtil.isEmpty(questionPublishForm.getTitle())) {
            message.setSuccess(false);
            message.setData("请输入文章标题");
            return message;
        }
        // 标题字数限制
        if (questionPublishForm.getTitle().length() <= 5) {
            message.setSuccess(false);
            message.setData("文章标题字数不得少于 5 个字");
            return message;
        }
        // 话题检查
        if (FunUtil.isEmpty(questionPublishForm.getTopics())) {
            message.setSuccess(false);
            message.setData("请为文章添加话题");
            return message;
        }
        // 文章内容检查
        if (FunUtil.isEmpty(questionPublishForm.getContent())) {
            message.setSuccess(false);
            message.setData("请输入文章内容");
            return message;
        }

        /*检查成功，添加问题*/
        // 设置该问题的用户
        try {
            questionPublishForm.setAsk_user_id(this.getSessionUserID(session)+"");
            Article article = articleService.publishArticle(questionPublishForm);
            message.setSuccess(true);
            message.setData("/article/" + article.getId());
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setData("服务器未知异常");
        }
        return message;
    }

    @RequestMapping("/{articleID}")
    public String showDetail(@PathVariable int articleID, Model model) {
        // TODO: 没有找到跳转404页面
        Article article = articleService.findArticleByID(articleID);
        model.addAttribute("article", article);
        return "article/detail";
    }

    /**
     * 分页显示文章列表
     */
    @RequestMapping("/page-{page}")
    public String articlePage(@PathVariable int page, Model model) {
        PageBean<Article> pageResult = articleService.findArticleByPage(page, 15);
        model.addAttribute("pageResult", pageResult);

        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.addOrder(Order.desc("discussCount"));
        PageBean<Topic> topicResult = topicService.findByPage(criteria, page, 5);
        model.addAttribute("hotTopic", topicResult.getList());
        return "article/list";
    }

    @RequestMapping(value = "/ajax/save_comment", method = RequestMethod.POST)
    @ResponseBody
    public Message saveComment(int articleID, String content, HttpSession session) {
        Message msg = new Message();
        if (FunUtil.isEmpty(content)) {
            msg.setSuccess(false);
            msg.setData("请输入评论内容");
            return msg;
        }
        try {
            // 保存评论
            ArticleComment comment = articleService.saveAnswerComment(content, this.getSessionUserID(session), articleID);
            msg.setSuccess(true);
            msg.put("id", comment.getId());
            msg.put("type", "article");
        } catch (Exception e) {
            e.printStackTrace();
            msg.setSuccess(false);
            msg.setData("参数错误，评论失败");
        }
        return msg;
    }
}